<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑字段</title>
</head>
<body>

<script type="text/html" id="form">
    <div class="mainBox">
        <div class="main-container">
            <input type="hidden" name="id" value="{{d.id}}">
            <blockquote class="layui-elem-quote">基础信息</blockquote>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <text title="必填项" style="color:red;">*</text>
                    标题名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name"  value="{{d.name}}" lay-verify="required" autocomplete="off"
                           placeholder="请输入"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">
                    <text title="必填项" style="color:red;">*</text>
                    关联内容</label>
                <div class="layui-input-block">
                    <div class="xmSelect"
                         data-name="type"
                         data-sourceType="route"
                         data-source="/admin.material.title/type"
                         data-textField="name"
                         data-valueField="value"
                         data-radio="true"
                         data-layVerify="required"
                         data-direction="down"
                         data-onchange="typeChange"
                         data-selected="{{d.type}}"
                    ></div>
                </div>
            </div>
            <div class="layui-form-item style1" style="display: none">
                <label class="layui-form-label">字段1名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="content[0]"   autocomplete="off"
                           placeholder="请输入"
                           value="{{ d.content.length>0?d.content[0]['name']:''}}"
                           class="layui-input">
                    <input type="hidden" name="contentIds[0]"
                           value="{{ d.content.length>0?d.content[0]['id']:''}}">
                </div>
                <label class="layui-form-label">字段2名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="content[1]"   autocomplete="off"
                           placeholder="请输入"
                           value="{{ d.content.length>1?d.content[1]['name']:''}}"
                           class="layui-input">
                    <input type="hidden" name="contentIds[1]"
                           value="{{ d.content.length>1?d.content[1]['id']:''}}">
                </div>
                <label class="layui-form-label">字段3名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="content[2]"   autocomplete="off"
                           placeholder="请输入"
                           value="{{ d.content.length>2?d.content[2]['name']:''}}"
                           class="layui-input">
                    <input type="hidden" name="contentIds[2]"
                           value="{{ d.content.length>2?d.content[2]['id']:''}}">
                </div>
                <label class="layui-form-label">字段4名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="content[3]"   autocomplete="off"
                           placeholder="请输入"
                           value="{{ d.content.length>3?d.content[3]['name']:''}}"
                           class="layui-input">
                    <input type="hidden" name="contentIds[3]"
                           value="{{ d.content.length>3?d.content[3]['id']:''}}">
                </div>
                <label class="layui-form-label">字段5名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="content[4]"   autocomplete="off"
                           placeholder="请输入"
                           value="{{ d.content.length>4?d.content[4]['name']:''}}"
                           class="layui-input">
                    <input type="hidden" name="contentIds[4]"
                           value="{{ d.content.length>4?d.content[4]['id']:''}}">
                </div>
                <label class="layui-form-label">字段6名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="content[5]"  autocomplete="off"
                           placeholder="请输入"
                           value="{{ d.content.length>5?d.content[5]['name']:''}}"
                           class="layui-input">
                    <input type="hidden" name="contentIds[5]"
                           value="{{ d.content.length>5?d.content[5]['id']:''}}">
                </div>
            </div>
            <div class="layui-form-item style2" style="display: none">
                <label class="layui-form-label">选择关联资料库</label>
                <div class="layui-input-block">
                    <div class="xmSelect"
                         data-name="content_ids"
                         data-sourceType="route"
                         data-source="/admin.basic.content/index"
                         data-params='{"search_param":{"basic_game_id":{"value":"{{d.game.basic_game_id}}","condition":"="},"status":{"value":1,"condition":"="}}}'
                         data-textField="name"
                         data-valueField="id"
                         data-direction="down"
                         data-selected="{{d.content_ids}}"
                    ></div>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">
                    浮窗说明</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入浮窗说明" name="description" id="LAY_demo" lay-verify="content" ></textarea>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="laytp-btn laytp-btn-primary laytp-btn-sm" lay-submit="" lay-filter="laytp-form">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="laytp-btn laytp-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</script>
<form class="layui-form" lay-filter="layui-form"></form>
<script>
    if (localStorage.getItem("staticDomain")) {
        document.write("<link rel='stylesheet' href='" + localStorage.getItem("staticDomain") + "/component/layui/css/layui.css?v=" + localStorage.getItem("version") + "'>");
        document.write("<script src='" + localStorage.getItem("staticDomain") + "/component/layui/layui.js?v=" + localStorage.getItem("version") + "'><\/script>");
        document.write("<script src='" + localStorage.getItem("staticDomain") + "/component/laytp/layuiConfig.js?v=" + localStorage.getItem("version") + "'><\/script>");
        document.write("<script src='" + localStorage.getItem("staticDomain") + "/component/jquery_3.3.1.js?v=" + localStorage.getItem("version") + "'><\/script>");
        document.write("<script src='" + localStorage.getItem("staticDomain") + "/component/jquery.tablednd.js?v=" + localStorage.getItem("version") + "'><\/script>");
    } else {
        document.write("<link rel='stylesheet' href='/static/component/layui/css/layui.css?v=" + localStorage.getItem("version") + "'>");
        document.write("<script src='/static/component/layui/layui.js?v=" + localStorage.getItem("version") + "'><\/script>");
        document.write("<script src='/static/component/laytp/layuiConfig.js?v=" + localStorage.getItem("version") + "'><\/script>");
        document.write("<script src='/static/component/jquery_3.3.1.js?v=" + localStorage.getItem("version") + "'><\/script>");
        document.write("<script src='/static/component/jquery.tablednd.js?v=" + localStorage.getItem("version") + "'><\/script>");
    }
</script>
<script>

    layui.use(['laytp','layedit','form'], function () {
        let $ = layui.jquery;
        let form = layui.form;
        var layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: "/admin.common/editUpload"
                , type: ''//默认post
            },
            uploadVideo: {
                url: "/admin.common/editUpload"
                ,accept:"m3u8"
                , type: ''//默认post
            }
        })
        //获取参数ID
        var id = facade.getUrlParam('id');
        if(!id){
            facade.error('参数ID不能为空');
            return false;
        }
        window.typeChange = function(params){
            console.log(params);
            if(params.arr.length===0){
                $(".style1").hide();
                $(".style2").hide();
            }else{
                if(params.arr[0]['value']===1){
                    $(".style1").show();
                    $(".style2").hide();
                }else{
                    $(".style2").show();
                    $(".style1").hide();
                }
            }
        }
        //获取参数ID
        var id = facade.getUrlParam('id');
        if (!id) {
            facade.error('参数ID不能为空');
            return false;
        }

        //获取数据，渲染到对应的节点上
        facade.ajax({
            route: "/admin.material.title/info",
            data: {id: id},
            successAlert: false,
            showLoading: true
        }).done(function (res) {
            if (res.code === 0) {
                layui.laytpl($("#form").html()).render(res.data, function (string) {
                    $("form").html(string);
                    layui.laytpForm.render();
                    form.render();
                });

                var index = layedit.build('LAY_demo', {
                    tool: [
                        'strong', 'italic', 'underline', 'del', '|', 'left', 'center', 'right', '|', 'image', 'video'
                    ],
                    height: 500,
                });

                form.verify({
                    content: function (value) {
                        return layedit.sync(index);
                    },
                });

                layedit.setContent(index, res.data.description);
            }
        });

        form.on('submit(laytp-form)', function (data) {
            var btnAnim = layui.button.load({elem: $(this)});
            data.field.status = $(this).attr("data-status")
            facade.ajax({
                route: '/admin.material.title/edit',
                data: data.field
            }).done(function (res) {
                if (res.code === 0) {
                    parent.layui.layer.close(parent.layui.layer.getFrameIndex(window.name));//关闭当前页
                    parent.layui.table.reload("laytp-table");
                }
                btnAnim.stop();
            }).fail(function () {
                btnAnim.stop();
            });
            return false;
        });
    })
</script>
</body>
</html>